package com.custom.headerpagerview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.Shader.TileMode;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * @description：自定义弧形View
 * @Author MRyan
 * @Date 2020/3/19 12:07
 * @Version 1.0
 */
public class CustomHeaderView extends View {
    private int mCustomHeight;
    private int mCustomWidth;
    private Paint mCustomPaint;
    private int mCustomColor;
    private int mRadius;
    private int mEndColor;
    private LinearGradient mLinearGradient;


    public CustomHeaderView(Context context) {
        super(context);
    }

    public CustomHeaderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initAttr(context, attrs);
        initPaint();

    }

    private void initPaint() {
        mCustomPaint = new Paint();
        mCustomPaint.setAntiAlias(true);
        mCustomPaint.setStyle(Paint.Style.FILL);
        mEndColor = Color.parseColor("#FFFFFF");
        mLinearGradient = new LinearGradient(mCustomWidth / 2, 0, mCustomWidth / 2, mCustomHeight, mCustomColor, mEndColor, Shader.TileMode.MIRROR);
        mCustomPaint.setShader(mLinearGradient);
    }

    private void initAttr(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomHeaderView);
        mCustomColor = typedArray.getColor(R.styleable.CustomHeaderView_Custom_color, mCustomColor);

        typedArray.recycle();
    }

    public CustomHeaderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, 0);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mCustomWidth = MeasureSpec.getSize(widthMeasureSpec);
        mCustomHeight = MeasureSpec.getSize(heightMeasureSpec);
        mRadius = mCustomWidth * 2;
        setMeasuredDimension(mCustomWidth, mCustomHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawCircle(mCustomWidth / 2, mCustomHeight - mRadius, mRadius, mCustomPaint);

    }
}
